clear
set mem 300m
capture log close
log using "$dataloc/Log/cpi", text replace


/* --------------------------------------------------------------- *
    Author: Ed Al-Hussainy
    Description:  CPI data
 * --------------------------------------------------------------- */
 
forvalues i = 1/10 { 
    tempfile tmp`i' 
}

/* --------------------------------------------------------------- *
    Estimate end of period CPI using monthly and quartely CPI data
 * --------------------------------------------------------------- */

* USE: readappender A B C
* Program reads in all files with extension A in directory B, appends them and saves a single output file C

capture program drop readappender
program readappender
    version 9.1  
    args filext dir outds 
    
    quietly {

        capture erase "`dir'/`outds'.dta"

        local list : dir "`dir'" files "`filext'" , nofail
        local listn : word count `list'

        cd "`dir'"  

        forvalues i = 1/`listn' {

            local fil : word `i' of `list'

            noisily di "Currently processing `fil' (file `i' of `listn')"

            insheet using "`fil'", names clear          

            if `i' == 1 {
                save `outds', replace
            }
            else if `listn' > 1 {
                append using `outds'
                save `outds', replace
            }
        } 
    }    
end

readappender *.txt "$dataloc/Data/IFS/$dataver/CPI" IFS_P

rename countrycode cncode
sort cncode

merge cncode using "$dataloc/data/CountryClassification2010", keep(cncode cn region incgr) uniqusing
keep if _merge == 3
drop _merge 

do "$codeloc/country_selector"

* note that timename values in annual dataset are set to blank by append statement

gen _q = substr(timename,1,2) if substr(timename,1,1) == "Q"
gen _m = substr(timename,1,3) if substr(timename,1,3) == "Dec"
keep if missing(timename) | _q == "Q4" | _m == "Dec"

gen year = substr(timecode,3,4)
destring year, replace

gen period = ""
replace period = "Y" if missing(timename)
replace period = "Q" if _q == "Q4"
replace period = "M" if _m == "Dec"

* use line 64...ZF for CPI
rename zf64zfconsumerpriceindex ifs_cpi
replace ifs_cpi = "" if ifs_cpi == ".."
destring ifs_cpi, replace

keep cncode year period ifs_cpi

reshape wide ifs_cpi, i(cncode year) j(period) string

* rescale CPI to 2005 = 100
preserve
   keep if year == 2005
   
   foreach i in ifs_cpiY {
      rename `i' `i'_05
   }  
   
   drop year
   sort cncode
   save `tmp1'
restore   

sort cncode

merge cncode using `tmp1', uniqusing
drop _merge
  
foreach i in ifs_cpiY {
   replace `i' = (`i'/`i'_05) * 100
} 

drop *_05

* use WDI/WEO annual CPI series to fill in the gaps
merge cncode year using "$dataloc/Output/WDI_WEOmetrics", sort keep(cncode year wdi_cpi weo_cpi)
tab _merge
drop _merge

foreach i of var *_cpi* {
   replace `i' = . if `i' <= 0
}   


* average annual CPI
gen _p = .
replace _p = ifs_cpiY
replace _p = wdi_cpi if missing(_p)
replace _p = weo_cpi if missing(_p)

* end of period CPI
gen _pe = .
replace _pe = ifs_cpiM
replace _pe = ifs_cpiQ if missing(_pe)
replace _pe = _p if missing(_pe)

/* --------------------------------------------------------------- *
    checks
 * --------------------------------------------------------------- */

/* check whether monthly/quarterly CPI series are centered around 2005=100
gen _t1 = _pe/_p if year == 2005
sum _t1
di "check CPI series for these countries (confirm hyperinflation periods):"
tab cncode if _t1 < 0.9 | _t1 > 1.1 & !missing(_t1)
*/

/* --------------------------------------------------------------- *
    save results
 * --------------------------------------------------------------- */

drop if year < $startyear - 1 
drop if year > $endyear

isid cncode year
sort cncode year

compress 

keep cncode year _p _pe ifs_cpiY wdi_cpi weo_cpi

label var ifs_cpiY "IFS CPI (2005=100)"

label var _p "CPI (Avg)"
label var _pe "CPI (End of Period)"

label data "CPI (Version: $dataver)"
save "$dataloc/Output/CPI", replace


/* end of code */
clear
log close
exit